package com.example.textview.PLC;

import android.util.Log;

import com.example.textview.util.HexDump;

import java.security.InvalidParameterException;

public class FX_TCP {


    private String fixAdress(String M) {
        // M=M.toUpperCase();
        String index = M.substring(1);
        String strHex = Integer.toHexString(Integer.parseInt(index));
        strHex = strHex.toUpperCase();
        StringBuilder sb = new StringBuilder(strHex);//构造一个StringBuilder对象
        for (int i = sb.length(); i < 8; i++) {
            sb.insert(0, "0");
        }
        strHex = sb.toString();


        sb = new StringBuilder();//构造一个StringBuilder对象
        for (int k = 0; k < 4; k++) {

            sb.insert(0, strHex.substring(k * 2, k * 2 + 2));
        }
        strHex = sb.toString();

        return strHex;
    }


    public String writeBool(String M, String Status) {

        String ID = "02FF0000";
        M = M.toUpperCase();
        String strHex = fixAdress(M);


        if (M.contains("M")) {
            ID = ID + strHex + "204D" + "0100";
        } else if (M.contains("Y")) {
            ID = ID + strHex + "2059" + "0100";

        }


        StringBuilder sb = new StringBuilder(Status);//构造一个StringBuilder对象

        for (int i = Status.length(); i < 2; i++) {

            sb.insert(1, "0");
        }

        ID = ID + sb.toString();

        return ID;

    }


    public String readBool(String M, String count_hex) {

        String reciveStr=null;
        String ID = "00FF0A00";
        M = M.toUpperCase();
        String adrHex = fixAdress(M);

        if (M.contains("M")) {
            ID = ID + adrHex + "204D";
        } else if (M.contains("Y")) {
            ID = ID + adrHex + "2059";

        } else if (M.contains("X")) {
            ID = ID + adrHex + "2058";

        }

        if (count_hex.length() < 2)
            count_hex += "0";


        ID = ID + count_hex + "00";


        return ID;


    }

    public String readWord(String adr,int count){

        String count_str= HexDump.toHexString((byte) count);
        String reciveStr=null;
        String ID = "01FF0A00";

        adr = adr.toUpperCase();
        String adrHex = fixAdress(adr);

        ID = ID + adrHex + "2044"+ count_str+"00";



        return  ID;


    }

    public String writeWord(String adr,short data){

        String data_str=HexDump.toHexString(data);
        if(data_str!=null) {
            data_str = data_str.toUpperCase();
            Log.d("hex", "hex:"+data_str);

            StringBuilder sb = new StringBuilder();//构造一个StringBuilder对象
            for (int k = 0; k < data_str.length()/2; k++) {

                sb.insert(0, data_str.substring(k * 2, k * 2 + 2));
            }
            data_str = sb.toString();
            Log.d("hex", "hex:"+data_str);
        }


        String ID = "03FF0000";
        adr = adr.toUpperCase();
        String strHex = fixAdress(adr);

        ID = ID + strHex + "2044" + "0100"+data_str;

        return ID;
    }

    public String writeFloat(String adr,float value)
    {
        String valueHex=  HexDump.float2HexString(value);

        if(valueHex!=null) {
            valueHex = valueHex.toUpperCase();
            Log.d("hex", "hex:"+valueHex);

            StringBuilder sb = new StringBuilder();//构造一个StringBuilder对象
            for (int k = 0; k < valueHex.length()/2; k++) {

                sb.insert(0, valueHex.substring(k * 2, k * 2 + 2));
            }
            valueHex = sb.toString();
            Log.d("hex", "hex:"+valueHex);
        }



        String ID = "03FF0000";
        adr = adr.toUpperCase();
        String strHex = fixAdress(adr);

        ID = ID + strHex + "2044" + "0200"+valueHex;

        byte[] bytes = hexStringToByteArray(ID);

        return ID;


    }


    public String readFloat(String adr){

        String return_Value=null;
        String result=null;
        return_Value=this.readWord(adr, (byte) 2);
        return return_Value;



    }




    private  int toByte(char c) {
        if (c >= '0' && c <= '9')
            return (c - '0');
        if (c >= 'A' && c <= 'F')
            return (c - 'A' + 10);
        if (c >= 'a' && c <= 'f')
            return (c - 'a' + 10);

        throw new InvalidParameterException("Invalid hex char '" + c + "'");
    }

    public  byte[] hexStringToByteArray(String hexString) {
        int length = hexString.length();
        byte[] buffer = new byte[length / 2];

        for (int i = 0; i < length; i += 2) {
            buffer[i / 2] = (byte) ((toByte(hexString.charAt(i)) << 4) | toByte(hexString
                    .charAt(i + 1)));
        }

        return buffer;
    }
}
